package com.xidian.zhiancloud.controller.systemset.usermanage;

import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.enums.UserResponseEnums;
import com.xidian.zhiancloud.model.syset.RoleList;
import com.xidian.zhiancloud.service.user.UserService;
import com.xidian.zhiancloud.service.user.usermanage.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 角色
 */

@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    RoleService roleService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    UserService userService;

    /**
     * 添加角色
     * @param name 角色名称
     * @param code 编码
     * @return 返回 ResultDTO 对象
     */
    @ResponseBody
    @RequestMapping(value = "/addRolePermissions")
    public ResultDTO addRolePermissions
        (@RequestParam("name") String name, @RequestParam("code") String code) {
        RoleList roleList = new RoleList();
        Long companyId = this.getSessionUserCompanyId();
        if (StringUtils.isNotBlank(name) || StringUtils.isNotBlank(code)) {
            roleList = roleService.selectByNameAndCode(companyId,name, code);
            if (roleList == null) {
                roleService.addRolePermissions(companyId,name, code);
            } else if (roleList != null){
                return ResultDTO.errorOf(UserResponseEnums.ROLE_ALREADY_EXISTS);
            }
        } else {
            return ResultDTO.errorOf(UserResponseEnums.ROLE_MESSAGE_NOT_NULL);
        }
        return ResultDTO.successOf(roleService.getRoleList(),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 编辑角色
     * @param id 角色Id
     * @param name 角色名称
     * @param code 角色编码
     * @return return 返回 ResultDTO 对象
     */
    @ResponseBody
    @RequestMapping(value = "/updateRolePermissions")
    public ResultDTO updateRolePermissions(
          @RequestParam(value = "id", required = false, defaultValue = "0") Integer id,
          @RequestParam("name") String name, @RequestParam("code") String code) {
        if (StringUtils.isNotBlank(name) || StringUtils.isNotBlank(code)) {
            if(id > 0){
                roleService.updateRolePermissions(id, name, code);
            }
        }
        return ResultDTO.successOf(roleService.getRoleList(),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除角色
     * @param id 角色Id
     * @return return 返回 ResultDTO 对象
     */
    @RequestMapping(value = "/deleteRolePer")
    public ResultDTO deleteRolePer(@RequestParam("id") Integer id){
        roleService.deleteRolePer(id);
        return ResultDTO.successOf(roleService.getRoleList(),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 用户赋予角色
     * @param roleids 角色ID
     * @param id 用户ID
     * @return return 返回 ResultDTO 对象
     */
    @RequestMapping("/insertRoleUser")
    public ResultDTO insertRoleUser
            (@RequestParam("roleids") String roleids,
             @RequestParam("id") String id){
        roleService.insertRoleUser(roleids,id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除角色中相应的用户
     * @param roleids 角色ID
     * @param id 用户ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/deleteRoleUser")
    public ResultDTO deleteRoleUser (
        @RequestParam("roleid") String roleid,
        @RequestParam("id") String id) {
        roleService.deleteRoleUser(roleid,id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }


}
